//
//  IGComActivityVC.m
//  jingdongfang
//
//  Created by BOE on 2018/5/8.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGComActivityVC.h"
#import "IGCommunityVC.h"
#import "IGActivityVC.h"
#import "IGCommunitySearchVC.h"

@interface IGComActivityVC ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *curBtn;///>当前选中的按钮
@property (nonatomic, strong) UIScrollView *rootScrollView;

@property (nonatomic, strong) IGCommunityVC *comVC;
@property (nonatomic, strong) IGActivityVC *actVC;

@end

@implementation IGComActivityVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.isNavgationBarHidden = YES;
    
    [self createUI];
}

- (void)createUI {
    [self addTopView];
    [self addRootScrollView];
}

- (void)addRootScrollView {
    //创建主滚动视图
    CGFloat height = KSCREENHEIGHT - NavigationMagin - SafeBottomMagin - 49;
    self.rootScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, NavigationMagin, KSCREENWIDTH, height)];
    self.rootScrollView.backgroundColor = [UIColor appColor];
    self.rootScrollView.delegate = self;
    self.rootScrollView.pagingEnabled = YES;
    self.rootScrollView.userInteractionEnabled = YES;
    self.rootScrollView.bounces = NO;
    self.rootScrollView.showsHorizontalScrollIndicator = NO;
    self.rootScrollView.showsVerticalScrollIndicator = NO;
    self.rootScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:self.rootScrollView];
    [self.rootScrollView setContentSize:CGSizeMake(KSCREENWIDTH*2, height)];
    
    _comVC = [[IGCommunityVC alloc] init];
    _comVC.view.frame = CGRectMake(0, 0, KSCREENWIDTH, height);
    [self.rootScrollView addSubview:_comVC.view];
    
    _actVC = [[IGActivityVC alloc] init];
    _actVC.type = IGActivityListTypeQuanZi;
    _actVC.view.frame = CGRectMake(KSCREENWIDTH, 0, KSCREENWIDTH, height);
    [self.rootScrollView addSubview:_actVC.view];
}

- (void)addTopView {
    UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, SafeTopMagin)];
    statusView.backgroundColor = [UIColor c3Color];
    [self.view addSubview:statusView];
    
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, SafeTopMagin, KSCREENWIDTH, 44)];
    self.topView.backgroundColor = [UIColor c3Color];
    [self.view addSubview:self.topView];
    [self createNameButtons];
    
    UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(KSCREENWIDTH - 54, 0, 44, 44)];
    [searchBtn setImage:[UIImage imageNamed:@"app_search_icon"] forState:UIControlStateNormal];
    [searchBtn setTapActionWithBlock:^{
        [UIViewController ig_pushViewControllerClass:[IGCommunitySearchVC class] animated:YES];
    }];
    [_topView addSubview:searchBtn];
}

- (void)createNameButtons
{
    CGFloat kWidthOfButtonMargin = -2;
    CGFloat kHeightOfTopScrollView = 44;
    UIImageView *shadowImageView = [[UIImageView alloc] init];
    shadowImageView.tag = 99;
    shadowImageView.backgroundColor = [UIColor c4Color];
    [self.topView addSubview:shadowImageView];
    
    NSArray *titleArr = @[@"话题", @"活动"];
    //顶部tabbar的总长度
    CGFloat topScrollViewContentWidth = 0;
    NSInteger count = titleArr.count;
    for (int i = 0; i < count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        CGSize textSize = [titleArr[i] sizeWithFont:[UIFont font16] constrainedToSize:CGSizeMake(self.topView.bounds.size.width, kHeightOfTopScrollView)
                                   lineBreakMode:NSLineBreakByTruncatingTail];
        //累计每个tab文字的长度
        topScrollViewContentWidth += kWidthOfButtonMargin+textSize.width;
        //设置按钮尺寸
        if (i == 0) {
            [button setFrame:CGRectMake(KSCREENWIDTH/2 - (textSize.width+30), 0, textSize.width+30, kHeightOfTopScrollView)];
            shadowImageView.frame = CGRectMake(kWidthOfButtonMargin, button.height-2, topScrollViewContentWidth, 2);
            shadowImageView.centerX = button.centerX;
            button.selected = YES;
            _curBtn = button;
        } else {
            [button setFrame:CGRectMake(KSCREENWIDTH/2, 0, textSize.width+30, kHeightOfTopScrollView)];
        }
        [button setTag:i+100];
        [button setTitle:titleArr[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont font16];
        [button setTitleColor:[UIColor c5Color] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor c4Color] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(selectNameButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.topView addSubview:button];
    }
}

- (void)selectNameButton:(UIButton *)sender {
    if (_curBtn.tag != sender.tag) {
        _curBtn.selected = NO;
        _curBtn = sender;
        _curBtn.selected = YES;
        UIImageView *shadowImageView = (UIImageView *)[self.view viewWithTag:99];
        shadowImageView.centerX = _curBtn.centerX;
    }
    NSInteger tag = _curBtn.tag;
    CGPoint point = _rootScrollView.contentOffset;
    point.x = KSCREENWIDTH*(tag-100);
    [_rootScrollView setContentOffset:point];
}

#pragma mark --<UIScrollViewDelegate>
//滚动视图释放滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.rootScrollView) {
        //调整顶部滑条按钮状态
        int tag = (int)scrollView.contentOffset.x/KSCREENWIDTH + 100;
        UIButton *button = (UIButton *)[self.view viewWithTag:tag];
        [self selectNameButton:button];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
